import axios from 'axios';

//路径为'@/apis/axiosInstance'
// 创建 axios 实例
const axiosInstance = axios.create({
    baseURL: 'http://localhost:8080', 
    timeout: 5000, // 请求超时时间
    withCredentials: true
});

// 请求拦截器
axiosInstance.interceptors.request.use(
    config => {
        const token = localStorage.getItem('auth_token');
        if (token) {
            config.headers.Authorization = `${token}`;
        }
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);

// 响应拦截器
axiosInstance.interceptors.response.use(
    response => {
        return response.data;
    },
    error => {
        if (error.response) {
            const { status } = error.response;
            if (status === 401) {

                window.location.href = '/login';
            }
        }
        return Promise.reject(error);
    }
);

export default axiosInstance;
